﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Deeper.Common;
using Deeper.Common.Web;
using Deeper.DataAccess;
namespace Deeper.Controls
{

	[ToolboxData("<{0}:CrudForm runat=server></{0}:CrudForm>")]
	public class CrudForm : Panel, IPostBackEventHandler
	{

		[Bindable(true)]
		[DefaultValue("")]
		[Localizable(true)]
		public string Title
		{
			get
			{
				return ViewState.GetFromViewState<string>("Title", string.Empty);
			}
			set
			{
				ViewState["Title"] = value;
			}
		}

		[Bindable(true)]
		[DefaultValue("")]
		[Localizable(true)]
		public string Status
		{
			get
			{
				return ViewState.GetFromViewState<string>("Status", string.Empty);
			}
			set
			{
				ViewState["Status"] = value;
			}
		}


		[Bindable(true)]
		[DefaultValue("/images/dialog/icon_dialog.gif")]
		[Localizable(true)]
		public string Icon
		{
			get
			{
				return ViewState.GetFromViewState<string>("Icon", "/images/dialog/icon_dialog.gif");
			}
			set
			{
				ViewState["Icon"] = value;
			}
		}
		[Bindable(true)]
		[DefaultValue(false)]
		[Localizable(true)]
		///是否显示
		public bool IsShow
		{
			get
			{
				return ViewState.GetFromViewState<bool>("IsShow", false);
			}
			set
			{
				ViewState["IsShow"] = value;
			}
		}

		protected override void Render(HtmlTextWriter writer)
		{

			if (IsShow)
			{
				if (this.Visible || DesignMode)
					HyControlHelper.RenderTop(writer, Title + Status, Icon);
				base.Render(writer);

				if (this.Visible || DesignMode)
				{
					writer.AddAttribute(HtmlTextWriterAttribute.Class, "btn");
					writer.RenderBeginTag(HtmlTextWriterTag.Div);

					string SaveHref = Page.ClientScript.GetPostBackEventReference(this, "save");
					writer.MakeButton("button icon_save", SaveHref, "保存");

					writer.Write("&nbsp;&nbsp;");
					writer.AddAttribute(HtmlTextWriterAttribute.Class, "button icon_cross");
					string CancelHref = Page.ClientScript.GetPostBackClientHyperlink(this, "cancel");
					writer.MakeButton("button icon_cross", CancelHref, "取消");

					writer.RenderEndTag();
				}
				if (this.Visible || DesignMode)
					HyControlHelper.RenderBottom(writer);
			}
			string btnAdd = Page.ClientScript.GetPostBackEventReference(this, "add");
			writer.MakeButton("button icon_add", btnAdd, "添加");

		}

		public delegate void AddItemEventHandler();
		[Description("添加记录事件"), Category("Action")]
		public event AddItemEventHandler AddItem;

		public delegate void SaveEventHandler();
		[Description("保存事件"), Category("Action")]
		public event SaveEventHandler SaveEvent;

		[Description("Postback事件")]
		public void RaisePostBackEvent(String eventArgument)
		{
			switch (eventArgument)
			{
				case "add":
					if (AddItem.IsNotNull()) AddItem();
					break;
				case "save":
					if (SaveEvent.IsNotNull()) SaveEvent();
					break;
				case "cancel":
					this.Hide();
					break;
			}
		}


		/// <summary>
		/// 显示
		/// </summary>
		public void Show()
		{
			this.IsShow = true;
		}
		/// <summary>
		/// 隐藏
		/// </summary>
		public void Hide()
		{
			this.IsShow = false;
		}

		/// <summary>
		/// 发生编辑
		/// </summary>
		/// <param name="model"></param>
		/// <param name="isShow"></param>
		/// <param name="actAfter"></param>
		/// <param name="actBefore"></param>
		public void GetEdit(IModel model, bool isShow = true, Action actAfter = null, Action actBefore = null)
		{
			if (actBefore.IsNotNull()) actBefore();
			model.SetData(this);
			if (actAfter.IsNotNull()) actAfter();
			Status = model.IsNewModel() ? "添加" : "编辑";
			this.IsShow = isShow;
		}

	}
}
